package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Seller;
import com.xxx.entity.query.SellerQuery;
import com.xxx.service.ISellerService;
import com.xxx.utils.Jwt;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-27
 */
@RestController
@RequestMapping("/seller")
@Tag(name = "卖家管理")
public class SellerController {

    @Autowired
    private Jwt jwt;
    @Autowired
    private ISellerService sellerService;

    @PostMapping("/add")
    @Operation(summary = "添加卖家")
    public Result add(@RequestBody Seller seller){
        Boolean flag = sellerService.save(seller);
        return flag ? Result.success("添加卖家成功") : Result.error("添加卖家失败");
    }

    @GetMapping("/delete/{sellerId}")
    @Operation(summary = "删除卖家")
    public Result delete(@PathVariable Integer sellerId){
        Boolean flag = sellerService.removeById(sellerId);
        return flag ? Result.success("删除卖家成功") : Result.error("不存在该卖家Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改卖家")
    public Result update(@RequestBody Seller seller){
        Boolean flag = sellerService.updateById(seller);
        return flag ? Result.success("修改卖家成功") : Result.error("修改卖家失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询卖家")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Seller> page = sellerService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据卖家名、卖家手机号、卖家Id条件查询卖家")
    public Result selectByCondition(@RequestBody SellerQuery sellerQuery){
        LambdaQueryWrapper<Seller> lqw = new LambdaQueryWrapper<>();
        lqw.like(sellerQuery.getSellerName()!=null&&!sellerQuery.getSellerName().equals(""),
                        Seller::getSellerName,sellerQuery.getSellerName())
                .eq(sellerQuery.getSellerPhone()!=null&&!sellerQuery.getSellerPhone().equals(""),
                        Seller::getSellerPhone,sellerQuery.getSellerPhone())
                .eq(sellerQuery.getSellerId()!=null,Seller::getSellerId,sellerQuery.getSellerId());
        IPage<Seller> page = sellerService.page(new Page<>(sellerQuery.getCurrent(), sellerQuery.getSize()),
                lqw);
        return Result.success(page);
    }

    @PostMapping("/login")
    @Operation(summary = "根据手机号和密码登录")
    public Result login (@RequestBody Seller seller){
        Map<String, Object> map = new HashMap<>();

        // 这里模拟通过用户名和密码，从数据库查询seller

        LambdaQueryWrapper<Seller> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Seller::getSellerPhone,seller.getSellerPhone()).eq(Seller::getSellerPassword,seller.getSellerPassword());
        Seller u = sellerService.getOne(lqw);
        if(u==null)
            return Result.error("手机号或密码错误");

        String token = jwt.createToken(seller.getSellerPhone());
        map.put("sellerId", u.getSellerId());
        map.put("sellerName",u.getSellerName());
        map.put("sellerPhone",u.getSellerPhone());
        map.put("sellerAvatar",u.getSellerAvatar());
        map.put("token",token);

        return Result.success(map);
    }
}

